<script setup lang='ts'>
  import { onMounted, reactive } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import { getDateArray, getPlayEndTime, getSummaries } from '@/utils'
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import serachUserId from '@/components/serachUserid/index.vue'
  import table2excel from 'js-table2excel';
  const poolOptions = [
    {type:2,label:'亏损池'},
    {type:3,label:'盈利池'},
  ]
  const poolIfDefault = [
    {type:1,label:'是'},
    {type:2,label:'否'},
  ]
  const action = reactive({
    total:0,
    list:[] as any[],
    statistics:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis'))
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    user_id:'',
    in_user_pool:'',
    pool_type:''
  })
  const handleDel = (row:any = {})=>{
    const { nickname='', in_user_pool_id=0 } = row
    ElMessageBox.confirm(`确定删除个人礼物池用户：${nickname}吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await luckDrawApi.wishWheelDelPoolUser({
        id:in_user_pool_id,
      });
      loading.close();
      ToastSuccess('删除成功');
      getList();
    })
    .catch(() => {});
  }
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await luckDrawApi.wishWheelUserPoolStat({
      date_st,
      date_ed,
      ...listQuery
    })
    loading.close()
    const { list=[], total=0, all={} } = res.data
    action.statistics = [all]
    action.total = total
    action.list = list.map((res:any)=>{
      const { prop_diamond_num=0, reward_diamond_num=0 } = res
      return {
        ...res,
        prop_diamond_num:Number(prop_diamond_num),
        reward_diamond_num:Number(reward_diamond_num)
      }
    })
  }
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中')
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await luckDrawApi.wishWheelUserPoolStat({
      page:1,
      date_st,
      date_ed,
      user_id:listQuery.user_id,
      limit:action.total
    })
    loading.close()
    const { list=[] } = res.data
    const data = list.map((res:any)=>{
      res['output_ratio'] = `${res.output_ratio}%`
      return res
    })
    const tHeader = [
      { title: '用户UUID', key: "uuid"},
      { title: '昵称', key: "nickname"},
      { title: '参与次数', key: "draw_num"},
      { title: '消耗钻石数', key: "prop_diamond_num"},
      { title: '产出钻石数', key: "reward_diamond_num"},
      { title: '回馈率', key: "output_ratio"},
      { title: '损益', key: "profit_loss"},
      { title: '最后参与时间', key: "last_at"},
    ]
    table2excel(
      tHeader,
      data,
      `许愿转盘个人池报表`)
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <serachUserId v-model:user_id="listQuery.user_id" />
        </div>
        <xmDatePicker v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <div class="label">礼物池类型：</div>
          <el-select v-model="listQuery.pool_type" placeholder="请选择礼物池类型">
            <el-option label="全部" :value="''"/>
            <el-option
              v-for="(item, index) in poolOptions"
              :key="index"
              :label="item.label"
              :value="item.type"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <div class="label">是否在个人池：</div>
          <el-select v-model="listQuery.in_user_pool" placeholder="请选择是否在个人池">
            <el-option label="全部" :value="''"/>
            <el-option
              v-for="(item, index) in poolIfDefault"
              :key="index"
              :label="item.label"
              :value="item.type"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="warning" @click="handleDownload">导出列表</el-button>
        </div>
      </div>
    </div>
    <el-table
      style="width: max-content; min-height: 80px"
      class="xm-TableCenter"
      :data="action.statistics"
      border
    >
      <el-table-column width="180" prop="user_num" label="参与人数" />
      <el-table-column width="180" prop="draw_num" label="参与次数" />
      <el-table-column width="180" prop="prop_diamond_num" label="总投入（钻石）" />
      <el-table-column width="180" prop="reward_diamond_num" label="总产出（钻石）" />
      <el-table-column width="180" prop="output_ratio" label="产出比例">
        <template #default="scoped">
          {{ scoped.row.output_ratio  }}%
        </template>
      </el-table-column>
    </el-table>
    <el-table
      style="margin-top:40px;"
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
      show-summary
      :summary-method="((param:any)=>getSummaries(param,[1,2],[{key:6,unit:'%'}]))"
    >
      <el-table-column width="120" prop="user_id" label="用户ID" />
      <el-table-column width="120" prop="uuid" label="用户UUID" />
      <el-table-column min-width="150" show-overflow-tooltip prop="nickname" label="昵称" />
      <el-table-column width="180" sortable prop="draw_num" label="参与次数" />
      <el-table-column min-width="120" sortable prop="prop_diamond_num" label="消耗钻石数" />
      <el-table-column min-width="120" sortable prop="reward_diamond_num" label="产出钻石数" />
      <el-table-column min-width="100" sortable prop="output_ratio" label="回馈率">
        <template #default="scoped">
          {{ scoped.row.output_ratio }}%
        </template>
      </el-table-column>
      <el-table-column min-width="120" sortable prop="profit_loss" label="损益" />
      <el-table-column width="180" prop="last_at" label="最后参与时间" />
      <el-table-column width="120" fixed="right" label="操作">
        <template #default="scoped">
          <el-button v-if="scoped.row.in_user_pool == 1" type="danger" @click="handleDel(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>
